function createBill()
{
    dojo.xhrGet({
        url: 'index.php/bill/bill/edit',
        load: function(data){
            var myCenter= dijit.byId("myCenter");
            myCenter.set('content', data);
            dojo.parser.parse(dojo.byId('myCenter'));
        }
    });
}

function saveBill()
{
    dojo.xhrPost({
        url: 'index.php/bill/bill/edit?format=json',
        form: 'billForm',
        load: function(data){
            console.log(data);
//            refreshAddressGrid();
        }
    });
}

function addProduct()
{
    var positionContainer = dojo.byId('billPositions');
    
    var id = positionContainer.childNodes.length;
    var position = document.createElement("tr");
    
    
    createPosition(position, positionContainer.childNodes.length);
    createPosition(position, '<input id="Description'+id+'" name="Description'+id+'" type="text">');
//    createPosition(position, '<input id="'+id+'">');
    createPosition(position, '<input id="Quantity'+id+'" name="Quantity'+id+'" type="text">');
    createPosition(position, '<input id="Price'+id+'" name="Price'+id+'" type="text">');
    createPosition(position, '<input id="Tax'+id+'" name="Tax'+id+'" type="text" value="19">');
    
    createPosition(position, '<input type="hidden" id="positionId'+id+'" name="positionId'+id+'"><button onclick="savePosition('+id+')">save</button>');
    
    
    positionContainer.appendChild(position);
//    var store = new dojo.data.ItemFileWriteStore({
//        url: 'index.php/product/product/list?format=json'
//    });
//    var filteringSelect = new dijit.form.FilteringSelect({
//            id: id,
//            name: "product_id",
//            store: store,
//            searchAttr: "product_name"
//        }, id);
//    dojo.connect(dijit.byId(id), 'onChange', function (val) {
//        productChanged();
//        console.log(val);
//        
//    });    
}

function savePosition(id)
{
    
//    console.log(dojo.byId('Description'+id).value);
    var postData = {'description': dojo.byId('Description'+id).value, 
            'price': dojo.byId('Price'+id).value,
            'bill_id': dojo.byId('bill_id').value,
            'quantity': dojo.byId('Quantity'+id).value,
            'tax': dojo.byId('Tax'+id).value,
            'position_id' : dojo.byId('positionId'+id).value
        };
    
    dojo.xhrPost({
        url: 'index.php/bill/bill/saveposition?format=json',
        postData: postData,
        load: function(data){
//            console.log(data);
            var msg = {};
            msg = JSON.parse(data);
            
//            console.log(msg);
            if(msg.message=='insert')
            {
                dojo.byId('positionId'+id).value = msg.posid;
            }
            
        }
    });
}

function createPosition(position, html)
{
    var row1 = document.createElement("td");
    row1.innerHTML = html;
    position.appendChild(row1);
}



function toggleDiv(div){
    if(   dojo.style(dojo.byId(div), "display") == "none"){
        dojo.style(dojo.byId(div), "display", "block");
        
    } else {
        dojo.style(dojo.byId(div), "display", "none");
        
    }
}

function productChanged()
{
    console.log("changed");
}

function openBill()
{
    var myCenter= dijit.byId("myCenter");
//    myCenter.set('content', createSplitContainer('Bill'));
    
    
    var store = new dojo.data.ItemFileWriteStore({
        url: 'index.php/bill/bill/list?format=json'
    });
    var layout = [
		    {'name': '#', 'field': 'bill_id', 'width': '40px'},
		    {'name': 'Kunde', 'field': 'address_name1', 'width': '200px'},
		    {'name': 'Datum', 'field': 'bill_date', 'width': '100px'}
		];
    var grid = dijit.byId("gridBill");
    if(!grid)
    {
        grid = new dojox.grid.DataGrid({
            id: 'gridBill',
            store: store,
            structure: layout,
            rowSelector: '20px'},
            document.createElement('div')
        );
        dojo.connect(grid, 'onRowDblClick', 'onBillGridDblClick');
        dijit.byId("myCenter").set( 'content', grid.domNode );
//        dijit.byId("BillBorderContainer").resize();
//        grid.startup();
       
    }
     
}
function onBillGridDblClick(e)
{
    console.log(e);
}

//function getBillForm()
//{
//    if(!dijit.byId('bilLForm'))
//    {   
//        dojo.xhrGet({
//            url:"index.php/bill/bill/edit",
//            handleAs:"text",
//            load: function(data){
//    //            console.log(data);
//                dijit.byId("bcProductForm").set('content', data);
//                initUploader();
//            }   
//        });
//
//        dojo.parser.parse(dojo.byId('bcProductForm'));
//        
//    }
//}
